package com.itheima.logdemo.web;

import com.itheima.logdemo.utils.LogInfo;
import com.itheima.logdemo.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class ApiService {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * service层方法调用，验证logbean信息的获取
     * threadlocal实现
     */
    @LogInfo
    public String getUser(int id) throws InterruptedException {

        //模拟一个耗时操作
        LogUtil.log("I am service");
        Thread.sleep(300);

        //打印before信息
        LogUtil.log("before call user");
        Thread.sleep(300);

        //跨服务调用开始
        String res = restTemplate.getForObject("http://user/info?id="+id,String.class);
        Thread.sleep(300);

        //调用后打印远程返回的结果
        LogUtil.log("after call user, res="+res);
        Thread.sleep(300);

        return res;
    }

    /**
     * service层登录操作
     */
    public String hystrixTest()  {
        try {
            System.out.println("service:"+Thread.currentThread().getName());
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "ok";
    }

}
